'This is the slave device program of the AWE Argus system
'The device is based on a picaxe shield card with a 28x2 controller
'and counts pulses from an energy meter (10000imp/kWh) and divides it with 10 (1000 imp / kWh)
'The pulse count can be read via i2c, address 0 (word)
'Connectors:
'Slaveboard  <-> Masterboard: connect C.3 and C.4 on both boards (i2c)
'Slaveboard <- External serial device C.7 (hserin)
'Slaveboard <- pulse sensor. B.0 (hint0)
'Power from masterboard (5V)

'Scratchpad allocation table:
' 0 -1 - Counter word




#picaxe 28x2
#no_data
#no_table

symbol DivCounter = b0	'temporary pulse counter variable (0-9)
symbol PlscntL = w3
symbol PlscntH = w4
symbol PlscntDivTen = w2 'Pulses / 10 (1000 pulses / kWh)
pullup %00000001
input B.0  'make B.0 an input
hintsetup  %00010001   'INT 0 on leading edge  
setintflags  %00000001,%00000001      'Enable INT0/ Pin B.0

hi2csetup I2cslave, $A0

main:

pause 5000
goto main

interrupt:
	inc DivCounter
	inc PlscntL
	put 0, word PlscntL
	
	if PlscntL = 0 then
		inc PlscntH
		put 4, word PlscntH
	endif
	
	'Divide pulses by 10
	if DivCounter = 10 then
		inc PlscntDivTen
		DivCounter = 0
		put 2, word PlscntDivTen
	endif

	flags = 0              'Clear all int flag bits... Bit 0 and bit 3 will both be set.
	setintflags %00000001,%00000001    ' Enable INT 0.  (hintsetup is not needed here) 
	return